package com.haredot.config;

import com.haredot.filter.BearTokenAuthenticationFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .maxAge(TimeUnit.SECONDS.toMillis(30));
    }

    @Bean
    public FilterRegistrationBean<BearTokenAuthenticationFilter> bearTokenAuthenticationFilterFilterRegistrationBean() {
        FilterRegistrationBean<BearTokenAuthenticationFilter> bearTokenAuthenticationFilterFilterRegistrationBean = new FilterRegistrationBean<>();

        bearTokenAuthenticationFilterFilterRegistrationBean.setFilter(new BearTokenAuthenticationFilter());
        return bearTokenAuthenticationFilterFilterRegistrationBean;
    }
}
